package com.hhhn.wk.main.fragment;
import android.content.ActivityNotFoundException;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hhhn.wk.R;
import com.hhhn.wk.base.BaseActivity;
import com.hhhn.wk.base.BaseFragment;
import com.hhhn.wk.entity.User;
import com.hhhn.wk.login.LoginActivity;
import com.hhhn.wk.main.tab4.mymoney.MyMoneyActivity;
import com.hhhn.wk.main.tab4.address.AddressAllActivity;
import com.hhhn.wk.main.tab4.CoupleBackActivity;
import com.hhhn.wk.main.tab4.PersonalActivity;
import com.hhhn.wk.main.tab4.SettingActivity;
import com.hhhn.wk.main.tab4.allorder.MyOrderActivity;
import com.hhhn.wk.utils.AllPublic;
import com.hhhn.wk.utils.Constants;
import com.hhhn.wk.utils.SharePreferencesUser;
import com.hhhn.wk.utils.ToastUtils;
import com.hhhn.wk.widget.customview.CircleImageView;
import com.hhhn.wk.widget.dialog.ActionSheet;
import com.tencent.mm.opensdk.modelbiz.JumpToBizProfile;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import static android.content.ContentValues.TAG;
import static com.hhhn.wk.utils.AllPublic.isLogin;
/**
 * A simple {@link Fragment} subclass.
 */
public class TabFragment04 extends BaseFragment {
    MBroadcastReceiver mBroadcastReceiver;//广播接受者
    private LinearLayout ll_set;//设置
    private LinearLayout ll_allOrder;//全部订单
    private LinearLayout ll_allOrder_dzf;//待支付
    private LinearLayout ll_allOrder_dfh;//待发货
    private LinearLayout ll_allOrder_dsh;//待收货
    private LinearLayout ll_allOrder_dpj;//待评价
    private LinearLayout ll_coupleBack;//意见反馈
    private LinearLayout ll_address;//收货地址
    private LinearLayout ll_lxkf;//联系客服
    private LinearLayout ll_personal_n;//去登录登录
    private LinearLayout ll_personal;//已登录/个人资料
    private LinearLayout ll_money;//我的余额
    private LinearLayout ll_gzh;//公众号

    private TextView tv_userNickName;//用户昵称
    private CircleImageView iv_head;//用户头像
    public TabFragment04() {
        // Required empty public constructor
    }
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1://更新
                    initData();
                    break;
//                case 2://加载图片
//                    Log.d(TAG, "加载图片: "+ user.getHeadPic());
//                    AllPublic.img(getContext(),avatarImageView,user.getHeadPic());
//                    break;
            }
        }
    };
    User user=null;
    @Override
    protected void initView(View mRootView) {
        ll_gzh=mRootView.findViewById(R.id.ll_gzh);
        ll_money=mRootView.findViewById(R.id.ll_money);
        iv_head=mRootView.findViewById(R.id.iv_head);
        tv_userNickName=mRootView.findViewById(R.id.tv_userNickName);
        ll_personal_n=mRootView.findViewById(R.id.ll_personal_n);
        ll_personal=mRootView.findViewById(R.id.ll_personal);
        ll_set=mRootView.findViewById(R.id.ll_set);
        ll_personal=mRootView.findViewById(R.id.ll_personal);
        ll_allOrder=mRootView.findViewById(R.id.ll_allOrder);
        ll_allOrder_dzf=mRootView.findViewById(R.id.ll_allOrder_dzf);
        ll_allOrder_dfh=mRootView.findViewById(R.id.ll_allOrder_dfh);
        ll_allOrder_dsh=mRootView.findViewById(R.id.ll_allOrder_dsh);
        ll_allOrder_dpj=mRootView.findViewById(R.id.ll_allOrder_dpj);
        ll_coupleBack=mRootView.findViewById(R.id.ll_coupleBack);
        ll_address=mRootView.findViewById(R.id.ll_address);
        ll_lxkf=mRootView.findViewById(R.id.ll_lxkf);
        mBroadcastReceiver = new MBroadcastReceiver();
        IntentFilter intentFilter = new IntentFilter();
        //设置接收广播的类型
        intentFilter.addAction(Constants.MBroadcastReceiver);
        //调用Context的registerReceiver（）方法进行动态注册
        mActivity.registerReceiver(mBroadcastReceiver, intentFilter);
    }
    @Override
    protected void initData() {
        user = (User) SharePreferencesUser.getBean(mActivity,Constants.USER);
        if (user!=null){
            int isLogin = user.getIsLogin();
            if (isLogin==2){
                ll_personal_n.setVisibility(View.GONE);
                ll_personal.setVisibility(View.VISIBLE);

                tv_userNickName.setText(user.getUserNickname());

                AllPublic.showImg(mActivity,user.getUserImg(),iv_head);
//                AllPublic.showImg(mActivity,"/upload/default/head.jpg",iv_head);

            }else {
                ll_personal_n.setVisibility(View.VISIBLE);
                ll_personal.setVisibility(View.GONE);
            }
        }else {
            ll_personal_n.setVisibility(View.VISIBLE);
            ll_personal.setVisibility(View.GONE);
        }
    }
    @Override
    protected void initClick() {
        //登录
        ll_personal_n.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(mActivity, LoginActivity.class));
            }
        });
        //余额
        ll_money.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (isLogin(mActivity, (BaseActivity) mActivity)) {
                    startActivity(new Intent(mActivity, MyMoneyActivity.class));
                    }
                }
            });
            //设置
            ll_set.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (isLogin(mActivity, (BaseActivity) mActivity)) {
                    startActivity(new Intent(mActivity, SettingActivity.class));
                    }
                }
            });
             //个人资料
            //已登录
            ll_personal.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (isLogin(mActivity, (BaseActivity) mActivity)) {
                    startActivity(new Intent(mActivity, PersonalActivity.class));
                    }
                }
            });
            //全部订单
            ll_allOrder.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (isLogin(mActivity, (BaseActivity) mActivity)) {
                    Intent intent=new Intent(mActivity,MyOrderActivity.class);
                    intent.putExtra("type",0);
                    startActivity(intent);
                    }
                }
            });
            //待支付
            ll_allOrder_dzf.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (isLogin(mActivity, (BaseActivity) mActivity)) {
                    Intent intent=new Intent(mActivity,MyOrderActivity.class);
                    intent.putExtra("type",1);
                    startActivity(intent);
                }
                }
            });
            //待发货
            ll_allOrder_dfh.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (isLogin(mActivity, (BaseActivity) mActivity)) {
                    Intent intent=new Intent(mActivity,MyOrderActivity.class);
                    intent.putExtra("type",2);
                    startActivity(intent);
                    }
                }
            });
            //待收货
            ll_allOrder_dsh.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (isLogin(mActivity, (BaseActivity) mActivity)) {
                    Intent intent=new Intent(mActivity,MyOrderActivity.class);
                    intent.putExtra("type",3);
                    startActivity(intent);
                    }
                }
            });
            //待评价
            ll_allOrder_dpj.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (isLogin(mActivity, (BaseActivity) mActivity)) {
                    Intent intent=new Intent(mActivity,MyOrderActivity.class);
                    intent.putExtra("type",4);
                    startActivity(intent);
                    }
                }
            });
            //意见反馈
            ll_coupleBack.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (isLogin(mActivity, (BaseActivity) mActivity)) {
                    Intent intent=new Intent(mActivity,CoupleBackActivity.class);
                    startActivity(intent);
                }
                }
            });
        //收货地址
        ll_address.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isLogin(mActivity, (BaseActivity) mActivity)) {
                    Intent intent=new Intent(mActivity,AddressAllActivity.class);
                    intent.putExtra("type",0);
                    startActivity(intent);
                }

            }
        });
        //联系客服
        ll_lxkf.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ActionSheet.showSheet(mActivity,"联系客服", new ActionSheet.OnActionSheetSelected() {
                    @Override
                    public void onClick(int whichButton) {
                        switch (whichButton) {
                            case 0:
//                showToast("点击了清除");
                                //打电话
                                try {
                                    Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+ Constants.contactUp));
                                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                    startActivity(intent);
                                }catch (ActivityNotFoundException exception){
                                    ToastUtils.showShortToast(mActivity,"当前设备不支持此功能");
                                }
                                break;
                            case 1:
//                showToast("点击了取消");
                                break;
                            default:
                                break;
                        }
                    }
                }, new DialogInterface.OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface dialogInterface) {

                    }
                });
            }
        });
        ll_gzh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String appId = "wx0242bc511b52ce4f";//开发者平台ID
                IWXAPI api = WXAPIFactory.createWXAPI(getActivity(), appId, false);

                if (api.isWXAppInstalled()) {
                    JumpToBizProfile.Req req = new JumpToBizProfile.Req();
                    req.toUserName = "gh_1fe96d20d5ad"; // 公众号原始ID
                    req.extMsg = "";
                    req.profileType = JumpToBizProfile.JUMP_TO_NORMAL_BIZ_PROFILE; // 普通公众号
                    api.sendReq(req);
                }else{
                    Toast.makeText(getActivity(), "微信未安装", Toast.LENGTH_SHORT).show();
                }
//                ClipboardManager tvCopy = (ClipboardManager)mActivity.getSystemService(Context.CLIPBOARD_SERVICE);
//                tvCopy.setText("huohuihuoniu");
//                    try {
//                        Intent intent = new Intent(Intent.ACTION_MAIN);
//                        ComponentName cmp = new ComponentName("com.tencent.mm","com.tencent.mm.ui.LauncherUI");
//                        intent.addCategory(Intent.CATEGORY_LAUNCHER);
//                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                        intent.setComponent(cmp);
//                        startActivity(intent);
//                    } catch (ActivityNotFoundException e) {
//                        // TODO: handle exception
//                        Toast.makeText(getActivity(), "微信未安装", Toast.LENGTH_SHORT).show();
//                    }
            }
        });
    }
    @Override
    protected int setLayoutResouceId() {
        return R.layout.fragment_tab_fragment04;
    }
    public class MBroadcastReceiver extends BroadcastReceiver {
        //接收到广播后自动调用该方法
        @Override
        public void onReceive(Context context, Intent intent) {
            //写入接收广播后的操作
            Log.d(TAG, "MBroadcastReceiver: 接收广播");
            handler.sendEmptyMessage(1);
        }
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        mActivity.unregisterReceiver(mBroadcastReceiver);
    }
}
